package com.example.wangning.mycarofcountry.recommend.journalism;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.mycarofcountry.entity.dao.Recommend_News;
import com.example.wangning.mycarofcountry.R;
import com.example.wangning.mycarofcountry.cache.VolleySingleton;
import com.example.wangning.mycarofcountry.tool.Player;

import java.util.List;

/**
 * Created by dllo on 16/1/12.
 */
public class JournalismAdapter extends RecyclerView.Adapter<JournalismAdapter.JournalismViewHolder>{
    private List<Recommend_News> resultEntityList;
    private VolleySingleton queue;
    private ImageLoader imageLoader;
    private Player player;
    private String minStr;
    private String secStr;
    private Handler handler;
    private int love_count;
    public JournalismAdapter(Context context) {
        queue = VolleySingleton.getVolleyIntance();
        imageLoader = queue.getImageLoader();
    }
    public void addData(List<Recommend_News> data){
        resultEntityList = data;
        notifyDataSetChanged();
    }

    @Override
    public JournalismViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View view = inflater.inflate(R.layout.recycler_journalism_item,null);
        return new JournalismViewHolder(view);
    }

    @Override
    public void onBindViewHolder(JournalismViewHolder holder, int position) {
        if (resultEntityList != null && resultEntityList.size() > 0){
            final Recommend_News resultEntity = resultEntityList.get(position);
            player = new Player(holder.skbProgress);
            if (resultEntity != null){
                holder.titleTv.setText(resultEntity.getText());//内容
                holder.timeTv.setText(resultEntity.getCreated_at());//发送时间
                holder.nameTv.setText(resultEntity.getName());//网名
                if (resultEntity.getVoicetime() != null && resultEntity.getVoicetime().length() > 0) {//进度

                    String musicTime = resultEntity.getVoicetime();
                    int time = Integer.parseInt(musicTime);
                    int min = time / 60;
                    int sec = (time - (min * 60));
                    if ((min < 0)) {
                        minStr = "0" + min;
                    } else {
                        minStr = "" + min;
                    }
                    if ((sec < 0)) {
                        secStr = "0" + sec;
                    } else {
                        secStr = "" + sec;
                    }
                    holder.musicTimeTv.setText(minStr + ":" + secStr);
                }
                if (resultEntity.getImage0() != null && resultEntity.getImage0().length() > 0){//图片
                    String imgUrl = resultEntity.getImage0();
                    getBitmap(imgUrl, holder.imageView);
                }
                if (resultEntity.getVoiceuri() != null && resultEntity.getVoiceuri().length() > 0) {//音乐路径

                    holder.stopIv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            player.pause();//暂停
                            player.stop();
                        }
                    });
                    holder.imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            player.playUrl(resultEntity.getVoiceuri());
                        }
                    });
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return resultEntityList != null ? resultEntityList.size() : 0;
    }
    private void getBitmap(String url, final NetworkImageView networkImageView) {
        imageLoader = queue.getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.cleak_repeat_normal);
        networkImageView.setErrorImageResId(R.mipmap.cleak_repeat_normal);
        networkImageView.setImageUrl(url, imageLoader);


    }
    class JournalismViewHolder extends RecyclerView.ViewHolder{
        TextView titleTv,timeTv,nameTv;
        NetworkImageView imageView;
        private ImageView stopIv;
        private TextView musicTimeTv;
        private SeekBar skbProgress;
        private boolean isLoveClick;
        public JournalismViewHolder(View itemView) {
            super(itemView);
            titleTv = (TextView) itemView.findViewById(R.id.titleTv);
            timeTv = (TextView) itemView.findViewById(R.id.timeTv);
            nameTv = (TextView) itemView.findViewById(R.id.nameTv);
            imageView = (NetworkImageView) itemView.findViewById(R.id.music_item_imageIv);
            musicTimeTv = (TextView) itemView.findViewById(R.id.musicTimeTv);
            stopIv = (ImageView) itemView.findViewById(R.id.stopIv);
            skbProgress = (SeekBar) itemView.findViewById(R.id.skbProgress);
            skbProgress.setVisibility(View.GONE);
            isLoveClick = false;
        }

    }

}
